/**
 * 角色模块服务文件
 */

const { validName, addRole, addMenus, updateRole, deleteRole, getRoleList, hasTeacherByRoleId, getRolesByTeacherId } = require('../db/roles');
const { getMenuList } = require('../db/menus');

/**
 * 添加角色
 * @param {*} ctx 上下文
 * @returns 
 */
const add = async (ctx) => {
    try {
        const role = ctx.request.body;
        const result = await validName(role);
        if (result) {
            return ctx.body = {
                code: 0,
                msg: '已存在相同的角色名称'
            }
        }
        await addRole(role);
        ctx.body = {
            code: 1,
            msg: '角色添加成功'
        }
    } catch (error) {
        ctx.app.emit('error', error);
        ctx.body = {
            code: 0,
            msg: '角色添加失败'
        }
    }
}

/**
 * 添加角色菜单
 * @param {*} ctx 
 */
const addRoleMenus = async (ctx) => {
    try {
        let { role_id, menu_id } = ctx.request.body;
        if (menu_id.length) {
            //如果有需要添加的菜单则判断是否有依赖菜单
            const list = await getMenuList();
            for (const id of menu_id) {
                const menu = list.find(c => c.id === id);
                if (menu.relation) {    //如果有依赖项
                    const codes = menu.relation.split(',');
                    for (const code of codes) {
                        const relationItem = list.find(c => c.code === code);    //根据依赖项的编码找到依赖项菜单
                        menu_id.push(relationItem.id);
                    }
                }
            }
        }
        menu_id = [...new Set(menu_id)];
        await addMenus(role_id, menu_id);
        ctx.body = {
            code: 1,
            msg: '角色菜单分配成功'
        }
    } catch (error) {
        ctx.app.emit('error', error);
        ctx.body = {
            code: 0,
            msg: '角色菜单分配失败'
        }
    }
}

/**
 * 修改角色
 * @param {*} ctx 
 */
const update = async (ctx) => {
    try {
        const { name, id } = ctx.request.body;
        const result = await updateRole(name, id);
        if (result === 0) {
            return ctx.body = {
                code: 0,
                msg: '已存在相同的角色名'
            }
        }
        ctx.body = {
            code: 1,
            msg: '角色修改成功'
        }
    } catch (error) {
        ctx.app.emit('error', error);
        ctx.body = {
            code: 0,
            msg: '角色修改失败，请稍后再试'
        }
    }
}

/**
 * 删除角色
 * @param {*} ctx 
 */
const deleteRoleById = async (ctx) => {
    try {
        const id = ctx.params.id;
        const result = await hasTeacherByRoleId(id);
        if (result) {
            return ctx.body = {
                code: 0,
                msg: '当前角色下还存在教师，请移除后再删除此角色'
            }
        }
        await deleteRole(id);
        ctx.body = {
            code: 1,
            msg: '角色删除成功'
        }
    } catch (error) {
        ctx.app.emit('error', error);
        ctx.body = {
            code: 0,
            msg: '角色删除失败，请稍后再试'
        }
    }
}


/**
 * 删除角色
 * @param {*} ctx 
 */
const getList = async (ctx) => {
    try {
        const list = await getRoleList();
        ctx.body = {
            code: 1,
            data: list,
        }
    } catch (error) {
        ctx.app.emit('error', error);
        ctx.body = {
            code: 0,
            msg: '角色列表获取失败，请稍后再试'
        }
    }
}

/**
 * 获取教师角色
 * @param {*} ctx 
 */
const getTeacherRoles = async ctx => {
    try {
        const list = await getRolesByTeacherId(ctx.request.params.id);
        ctx.body = {
            code: 1,
            data: list.map(c => c.role_id)
        }
    } catch (error) {
        ctx.app.emit('error', error);
        ctx.body = {
            code: 0,
            msg: '教师角色获取失败，请稍后再试'
        }
    }
}

module.exports = {
    add,
    update,
    getList,
    addRoleMenus,
    deleteRoleById,
    getTeacherRoles
}